package com.dingwen.treasure.base.util;


import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 * 　FileUtil
 * 　@author dingwen
 * 　@date 2022/8/2
 */
@UtilityClass
public class FileUtil {
    /**
     * 获取当前工程文件全路径
     *
     * @return {@link String}
     */
    public  String getRootPullClassPath() {
        return FileUtil.class.getResource(File.separator).getPath();
    }


    /**
     * 文件转换为字节数组输出流
     *
     * @param file 文件
     * @return {@link ByteArrayOutputStream}
     */
    @SneakyThrows(Throwable.class)
    public  ByteArrayOutputStream fileToByteArrayOutputStream(File file) {
        @Cleanup
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        @Cleanup
        FileInputStream fileInputStream = new FileInputStream(file);

        byte[] buff = new byte[1024];
        int index;
        //4、执行 写出操作
        while ((index = fileInputStream.read(buff)) != -1) {
            byteArrayOutputStream.write(buff, 0, index);
            byteArrayOutputStream.flush();
        }
        return byteArrayOutputStream;

    }
}
